package com.qianfeng.shopping.dao;

import com.qianfeng.shopping.entity.OrderList;
import com.qianfeng.shopping.util.GetTime;
import com.qianfeng.shopping.util.GetUuid;

public class OrderListDaoImp implements OrderListDao {
	// 创建存放订单的数据
	static OrderList[] arr = new OrderList[100];
	@Override
	public boolean buy(OrderList orderList,int way) {
		Discount discount = null;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i]==null){
//				orderList.setOrderListId("orderList"+(i+1));
				//  以当前时刻作为订单id
//				String timeId = GetTime.getNow("yyyyMMddHHmmssSSS");
				//  利用uuid作为订单id
				orderList.setOrderListId(GetUuid.getUuid());
				// 计算总价  开发中写在业务逻辑层
				double totalPrice = orderList.getGoods().getGoodsPrice()*orderList.getAmount();
				orderList.setTotalPrice(totalPrice);
				//  根据折扣方案计算打折
				double payMoney = totalPrice;
				if (way==1){
					discount = new DiscountImp1();
					// 打折后的钱
					payMoney = discount.discountMoney(totalPrice,orderList.getAccount());
				}else if(way==2){
					discount = new DiscountImp2();
					// 打折后的钱
					payMoney = discount.discountMoney(totalPrice,orderList.getAccount());
				}
				orderList.setPayPrice(payMoney);
				// 获取当前系统时间
				orderList.setOrderListTime(GetTime.getNow("yyyy-MM-dd HH:mm:ss"));
				arr[i] =orderList;
				return true;
			}
		}
		return false;
	}

	@Override
	public OrderList[] selectAll() {
		return arr;
	}
}
